package org.jingle.sketch
{
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	
	import org.jingle.sketch.component.Arc;
	import org.jingle.sketch.component.Composite;
	import org.jingle.sketch.component.Point;
	import org.jingle.sketch.component.Shape;
	import org.jingle.sketch.event.AnchorDraggedEvent;
	
	public class Anchor implements Shape
	{
		/**
		 * left join point 
		 */		
		public static var LEFT_JOIN:int = 1;
		
		/**
		 * right join point
		 */
		public static var RIGHT_JOIN:int = 2;
		
		/**
		 * up join point
		 */
		public static var UP_JOIN:int = 3;
		
		/**
		 * down join point
		 */
		public static var DOWN_JOIN:int = 4;
		
		/**
		 * default order is (LEFT - RIGHT) or (UP - DOWN)
		 */
		public static var TWO_SPOT:int = 5;
		
		/**
		 * radious of anchor
		 */
		private var _radious:Number = 3; 
		
		/**
		 * line colour of anchor
		 */
		private var _color:uint = 0x000000;
		
		private var _conjoints:Array = new Array();

		/**
		 * position of anchor
		 */
		private var _origin:Point;
		
		/**
		 * shape of anchor
		 */ 
		private var _component:Composite;
		
		/**
		 * parent of anchor
		 */
		private var _owner:Composite;
		
		private var _type:int;
		
		private var _rangeStart:int;
		
		private var _rangeEnd:int;
		
		/**
		 * 
		 */
		private var _initialized:Boolean = false;
		
		public function Anchor(owner:Composite, p:Point, type:int, radious:int = 3, rangeStart:int = -10000, rangeEnd:int = 10000)
		{
			this._origin = p;
			this._owner = owner;
			this._rangeStart = rangeStart;
			this._rangeEnd = rangeEnd;
			_radious = radious;
			owner.addAnchor(this);
			_component = new Arc(owner, p, _radious, 360);
			_component.lineColor = _color;
			_component.fillColor = 0xffffff;
			//_component.sprite.rotation = owner.angle;
			this._type = type;
			_component.sprite.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
			_component.sprite.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
		}
		
		public function init():void {
			_component.paint();
			_initialized = true;
		}
		
		protected function mouseDownHandler(e:MouseEvent):void{
			e.stopPropagation();
			//var rect:Rectangle = new Rectangle(0, origin.y, 100000, 0);
			var rect:Rectangle = new Rectangle(_rangeStart, origin.y, _rangeEnd, 0);
			Sprite(this.sprite).startDrag(false,rect);
		}
		
		protected function mouseUpHandler(e:MouseEvent):void{
			Sprite(this.sprite).stopDrag();
		}
		
		public function get owner():Composite {
			return this._owner;
		}
		
		public function get conjoints():Array {
			return this._conjoints;
		}
		
		public function join(s:Shape):void {
			_conjoints.push(s);
		}
		
		public function get shape():Shape {
			return this;
		}
		
		public function get sprite():Sprite {
			return _component.sprite;
		}
		
		public function get parent():Composite {
			return owner.parent;
		}
		
		public function get origin():Point {
			return _origin;
		}
		
		public function moveTo(p:Point):void {
			this._origin = p;
			sprite.x = origin.x;
        	sprite.y = origin.y;
		}
		
		public function paint():void {
			if(!_initialized){
				init();
			}
		}
		
		public function set color(c:uint):void {
			this._color = c;
			_component.lineColor = c;
			_component.fillColor = c;
		}
		
		public function get color():uint {
			return this._color;
		} 
		
		public function get type():int {
			return this._type;
		}
		
		public function anchorDragged(evt:AnchorDraggedEvent):void {
			
		}
	}
}